<html >
<head>
    <meta charset="UTF-8">
    <title>SSE Demo</title>
</head>
<body>
<button type="button" onclick="closeSse()">Close</button>
<label id="clientId"></label>
<hr style="margin: 2px; padding: 0px 0px;"/>
<ul id="list"></ul>
</body>
<script type="application/javascript">
    const nowTimeStamp = Date.now();
    const clientIdElement = document.getElementById("clientId")
    clientIdElement.innerHTML = nowTimeStamp;
    const evtSource = new EventSource(`/sse/event/${nowTimeStamp}`) ;
    evtSource.onmessage = (event) => {
        const newElement = document.createElement("li") ;
        const eventList = document.getElementById("list") ;
        newElement.innerHTML = "接收到消息: " + event.data ;
        eventList.appendChild(newElement) ;
    };
    evtSource.onopen = (event) => {
        console.log('建立连接...')
    };
    evtSource.onerror = (event) => {
        console.error("发生错误：", event) ;
    };
    function closeSse() {
        evtSource.close() ;
    }
</script>
</html>